<template>
  <!-- <div style="width: 100%;height: 100%;">
    <div id="main" ref="main"></div>
  </div> -->
  <div id="main" ref="main"></div>
</template>

<script lang="ts" setup>
import { onMounted, ref, defineProps, toRefs, watch } from "vue";
import * as echarts from "echarts";
import { echartsAutoPlay } from "@/utils/echarts";
const main = ref();

const props = defineProps({
  dataList:{
    type: Array
  }
})
const { dataList } = toRefs(props);
const data = ref([])

watch(
  dataList,
  (e) => {
    console.log(e)
    data.value = e;
    setTimeout(()=>{
      setEcharts();
    },1000)
  },
  {
    immediate: true,
  }
);

onMounted(() => {
  // setEcharts();
});
function setEcharts() {
  if(data.value.length > 0){
    var dateList = []
    var dList = []
    var wList = []
    var zList = []
    data.value.forEach((item,index) => {
      dateList.push(item.date)
      dList.push(item.d)
      wList.push(item.w)
      zList.push(item.z)
    });
    var myChart = echarts.init(main.value);
  }
  
  var option = {
    tooltip: {
      trigger: "axis",
      extraCssText: "z-index:999",
      confine: true,
      backgroundColor: "rgba(255,255,255,0.8)",
    },
    grid: {
      top: "20%",
      left: '5%',
      right: "5%",
      bottom: "0",
      containLabel: true,
    },
    xAxis: {
      type: "category",
      boundaryGap: false,
      data: dateList,
      axisLabel: {
        rotate: 15,
        textStyle: {
          color: "#CBD2D5",
        },
        interval: 0,
        margin: 20,
      },
      axisLine: {
        lineStyle: {
          color: "#CCCCCC",
        },
      },
      axisTick: {
        show: false,
      },
    },
    yAxis: {
      type: "value",
      minInterval: 1,
      axisLabel: {
        textStyle: {
          color: "#CBD2D5",
        },
      },
      splitLine: {
        lineStyle: {
          color: "#222324",
        },
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
    },
    series: [
      {
        data: dList,
        type: "line",
        smooth: true,
        name: '下降量',
        lineStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: "#FF7070" },
            { offset: 1, color: "#F42C2C" },
          ]),
        },
        itemStyle: { opacity: 0 },
      },
      {
        data: wList,
        type: "line",
        name: '无变化量',
        smooth: true,
        lineStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: "#21B1D1" },
            { offset: 1, color: "#66E8B2" },
          ]),
        },
        itemStyle: { opacity: 0 },
      },
      {
        data: zList,
        type: "line",
        name: '上升量',
        smooth: true,
        lineStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: "#FAC858" },
            { offset: 1, color: "#FFA508" },
          ]),
        },
        itemStyle: { opacity: 0 },
      },
    ],
  };

  myChart.setOption(option);
  echartsAutoPlay(option, myChart);
}
</script>

<style lang="less" scoped>
#main {
  width: 100%;
  height: 100%;
}
</style>
